Project

您所在的位置:网站首页 jenkins gitlab ci Project

Project

2023-04-10 07:20| 来源: 网络整理| 查看: 265

This Article is a simple approach towards developing a CI/CD pipeline to automate deployment cycle using Jenkins, Docker, Git GitHub Webhook, EC2 for a webapp based on django. Sharing the process in the following steps.First launched a public EC2 instance (t2.micro for free tier) with the ubuntu AMI offered by AWS.No alt text provided for this image

2. Create an IAM role with AmazonSSMManagedInstanceCore permission and attach to the instance so that we can directly connect to the server using SSM connect, or we can use putty to ssh in to the server (For that, the security group should allow port 22 for the source IP ). Also create a security group and allow port 80 and 8080 to all traffic (0.0.0.0/0) and attach it to the instance.

3. To setup the environment on the server, install git, docker and Jenkins on the server. THe commands for the same are as follows: sudo apt-get install git-all -- To install git. sudo apt install docker.io -- To install docker sudo snap install docker -- To install docker dependencies To install Jenkins and JDK please follow the following links https://phoenixnap.com/kb/install-jenkins-ubuntu

4. Now we first need to clone our code for the webApp from my git Repository. Link for the repository is:- https://github.com/PiyushPatil9111/django-todo-app.git Now Clone the code from the repository using the command sudo git clone https://github.com/PiyushPatil9111/django-todo-app.git

5. The todo list webApp code has multiple dependencies such as python, django etc. to run on the server.

6. To deal with the dependencies of the application we will create a Dockerfile and build an image using which the docker container will run the webapp.

Create a Dockerfile inside the django-todo-app directory and the content of the dockerfile is given below in the screensot

No alt text provided for this image

The Docker file contains the commands to - Pull python image from dockerhub. - Install django 4.1 on the container. - Copy the code pulled from github on the container. - Run python migrate command and run the service on port 80 on the container oprn to all source IPs coming from the host EC2.

7. Run the "sudo docker build . dockerimagename" command to build the docker image from the Dockerfile. Run the "sudo docker run -it -d -p 80:80 dockerimagename" command to run the container and map its port 80 with the port 80 of the host EC2. Now the Todo-App webapp should be accessible on publicip:80.

8. Once there are any changes done on the code, the same change should be deployed on the container. To automate this process, we will use jenkins setup on the host machine.

9. Login to Jenkins and create a new Job eg. todo_app_piepline. Add your github repository URL and select GitHub hook trigger for GITScm polling in build triggers and specify your git branch name.

No alt text provided for this image

Now Select execute shell in build trigger option and insert commands as per the screenshot.

No alt text provided for this image

The shell will remove the older container and its image and build the updated Dockerfile and create a new container with the image with port 80 exposed to hosts's port 80.

11. To automate job build everytime the changes are pushed to your Git repository follow: Go to Dashboard > Admin (or your user) > API Token > Generate API token and save it.

10. Go to your Repository in GitHub > settings > webhooks > Add webhooks Provide your Jenkins Url eg. http://13.232.118.1:8080/github-webhook/ . Provide the jenkins api token as the secret for webhook and save.

No alt text provided for this image

11. Now everytime any changes are pushed into your repository, your jenkins job will automatically run and update your container with the updated code.

No alt text provided for this image

12. For further improvements, we can put the Ec2 instance in front of an Application load balancer, register a private/public domain name and route traffic to the ALB using Route53.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3